/*
 * Copyright (C) 拾光分享网 
 * All rights reserved, Designed By www.hiai.top.
 * Email：goodsking@163.com.
 * Version：V1.0.2(20230701).
 */
package top.hiai.my.article;

import com.jfinal.core.Controller;
import com.jfinal.validate.Validator;

import top.hiai.common.util.SensitiveWordsKit;

/**
 * @Author www.hiai.top
 * @Email goodsking@163.com
 * @Message 更多资源尽在www.hiai.top
 * @Introduction 类的介绍
 */

public class MyArticleValidator extends Validator {

    @Override
    protected void validate(Controller c) {
        setShortCircuit(true);

        //验证敏感词
        checkSensitiveWords(c.getPara("article.title"), "文章标题 title 包含敏感词");
        checkSensitiveWords(c.getPara("article.content"), "文章内容 content 名包含敏感词");
        //验证不能为空
        validateString("article.title", 3, 100, "msg", "标题长度要求在3到100个字符");
        validateRequired("article.type_id", "msg", "分类不能为空");
        validateString("article.content", 19, 65536, "msg", "正文内容太少啦,不能少于20个字，多写点哈");
    }

    /**
     * 敏感词检测
     */
    private void checkSensitiveWords(String value, String msg) {
        if (SensitiveWordsKit.checkSensitiveWord(value) != null) {
            addError("msg", msg);
        }
    }

    @Override
    protected void handleError(Controller c) {
        c.renderJson();
    }
}